//
//  HMStatusListViewModel.swift
//  Faner_Weibo
//
//  Created by 范英强 on 16/2/1.
//  Copyright © 2016年 范英强. All rights reserved.
//

import UIKit

class HMStatusListViewModel: NSObject {
    
    var statusList: [HMStatusViewModel]?
    
    func loadData(isPullUp: Bool ,compelete:(isSuccess: Bool)->()){
        
        var maxid = isPullUp == true ? (statusList?.last?.status?.id ?? 0) : 0
        
        if maxid > 0 {
            maxid -= 1
        }
        
        HMNetWorkTools.sharedTools.loadStatusData(HMUserAccountViewModel.sharedAccount.userAccount!.access_token!,maxid: maxid) { (response, error) -> () in
            
            print(response)
            if error != nil {
                print("加载数据失败")
                compelete(isSuccess: false)
                return
            }
            
            guard let responseDict = response as? [String: AnyObject] else{
                print("不是字典")
                return
            }
            
            guard let result = responseDict["statuses"] as? [[String: AnyObject]]  else{
                return
            }
            
            var tempArray = [HMStatusViewModel]()
            
            for value in result {
                let status = HMStatus(dict: value)
                tempArray.append(HMStatusViewModel(status: status))
            }
            if self.statusList == nil {
                self.statusList = [HMStatusViewModel]()
            }
            self.statusList = self.statusList! + tempArray
            print(self.statusList)
            compelete(isSuccess: true)
            
        }
    }

}
